package com.example.examinationtest;

import java.util.Vector;
 


import com.example.examinationtest.*;
import  android.widget.Toast;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.renderscript.Sampler.Value;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	  long mil=900000;
	  TextView txtTimer;
	  CountDownTimer Timer;
 static int i=0,pos=1;
 static int ipo=1;
 static int j=0,m=0;
  String[] listQuestion;
  String[] listAnswer,listCorrectAnswer,parts,correctParts;
  CorrectAnswer[] Canswer;
  Vector  checkCorrect=new Vector(),checkWrong =new Vector(),correctAnswer1=new Vector();
  String answer,question,correctAnswer;
  String position;
  
  int count=0,countWrong=0;
  int result=0;
  int toTalQuestion=0;
  boolean correct=false;
  Vector Status= new Vector();
  boolean check =false;	        
  static int previous=0;
  
  @Override
  
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listQuestion=getResources().getStringArray(R.array.Question);
		toTalQuestion=listQuestion.length;
		listAnswer=getResources().getStringArray(R.array.Answer);
		listCorrectAnswer=getResources().getStringArray(R.array.Result);
		final TextView txtShow=(TextView)findViewById(R.id.textView2);
		final RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
		final RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
		final RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
		final RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
		 txtTimer=(TextView)findViewById(R.id.Timer);
		 final TimerCounting Timer= new TimerCounting(mil,10); 
		  final Button btnPrevious=(Button)findViewById(R.id.Previous);
 		 btnPrevious.setEnabled(false);
 //		 rd2.setChecked(true);
		 
 	
 		 
 		 
 		 Timer.start();		       
        // rd1.setSelected(false);

		 Canswer=loadPosition();
	/*	 String print="";
			for(int i=0;i<listCorrectAnswer.length;i++)
	        {
				print=print+Canswer[i].getQuestionCorrect()+" "+Canswer[i].getAnswerCorrect()+" "+Canswer[i].getPosition();
		
		     }	
			 txtShow.setText(print);
			   
	*/
		
		
	
		
		
			
		        
		loadQuestion(i);
		loadAnswer(i);
		
		 final Button btnNext=(Button)findViewById(R.id.Next);
		 
		 btnNext.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				   btnPrevious.setEnabled(true);   		        
			
					 	
					
				     RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
					 RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
					 RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
					 RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
					 final Button btnNext=(Button)findViewById(R.id.Next);
			if (rd1.isChecked()||rd2.isChecked()||rd3.isChecked()||rd4.isChecked())
			{	//TexView txtShow=(TextView)findViewById(R.id.textView2);
			 //   txtShow.setText("Result :"+result+"/"+toTalQuestion);
          
					
				
				i=i+1;
				j=j+4;
					
				
				
				// TODO Auto-generated method stub
if((i>0 && i<listQuestion.length)&&(j>0 &&j<listAnswer.length))
{
loadQuestion(i);	
loadAnswer(j);

}

//if(check==false)
//{

//}
//if (check==false)
check(ipo);
ipo=ipo+1;
checkRadio();



//txtShow.setText(i+"");

//	txtShow.setText(Status[i]);
		

	
	

if((i==listQuestion.length))
{
	checkResult();
	String value="";
    java.util.Iterator iter= checkCorrect.iterator();
	while(iter.hasNext()){
	value=value+(String)iter.next();  



	}
	String value1="";
	java.util.Iterator iter1= checkWrong.iterator();
	while(iter1.hasNext()){
	value1=value1+(String)iter1.next();  
	}
	
	
	/*
		String value2="";
	java.util.Iterator iter2= correctAnswer1.iterator();
	while(iter2.hasNext()){
	value2=value2+(String)iter2.next();  
	}*/
//for(int i=0;i<value.length();i++)
//{
//	correctParts=value.split(" ");
	
//}

//for(int i=0;i<correctParts.length;i++ )	
//{
//	value=correctParts[i]+value;
//}

	//txtShow.setText("R :" + value.toString()+"W :"+value1.toString());

   Intent intent=new Intent(MainActivity.this,Result.class);
   intent.putExtra("CorrectAnswer", value.toString());
   intent.putExtra("CorrectsNumber", count+"");
   intent.putExtra("WrongsNumber", countWrong+"");
   intent.putExtra("WrongAnswer", value1.toString());

   intent.putExtra("Total", toTalQuestion+"");
    
   startActivity(intent);
	
    //intent.putExtra("WrongAnswer", value1.toString());
    //finish();
	
	
	
    //txtShow.setText("Result : "+result+"/"+toTalQuestion);
//	rd1.setEnabled(false);
//	rd2.setEnabled(false);
//	rd3.setEnabled(false);
//	rd4.setEnabled(false);
	//btnNext.setEnabled(false);
	Timer.cancel();		

}
}
//RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
//rb.clearCheck();
			else
			{
				Toast.makeText(MainActivity.this, "Please select your answer", Toast.LENGTH_LONG).show();
			}

			}
			
		});
	
  
  // Previous event
	 btnPrevious.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
		// txtShow.setText("Result :"+result+"/"+toTalQuestion);
			//	check();
         if(i>=1){			
			String value1="";
			i=i-1;
			j=j-4;
			ipo=ipo-1;
			checkRadio();
			
			//check(ipo);
			//	txtShow.setText(i+"");
			
			
			// TODO Auto-generated method stub
if((i>=0 && i<=listQuestion.length)&&(j>=0 &&j<=listAnswer.length))
{
loadQuestion(i);	
loadAnswer(j);

}

//Status.removeAllElements();
//RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
//rb.clearCheck();
check=true;
if((i==-1))
{
rd1.setEnabled(false);
rd2.setEnabled(false);
rd3.setEnabled(false);
rd4.setEnabled(false);
//btnNext.setEnabled(false);
Timer.cancel();		
}
         }


		}
	});
}

  void checkRadio()
  {	final TextView txtShow=(TextView)findViewById(R.id.textView2);
	
		final RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
		final RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
		final RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
		final RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
	if(ipo<=Status.size())
	{
	  if (Status.get(i).equals("1"))
		{
			rd1.setChecked(true);
		}
		else if(Status.get(i).equals("2"))
		{
			rd2.setChecked(true);
				
		}
		else if(Status.get(i).equals("3"))
		{
			rd3.setChecked(true);
				
		}
		else if(Status.get(i).equals("4"))
		{
			rd4.setChecked(true);
				
		}
	}
	else {
	
		RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
		rb.clearCheck();
		
		
		
	}
//    txtShow.setText("Ipo"+ipo +" : Status"+Status.size());
		
  }
  
  
  
   void check(int position)
  {
  RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
  
	final TextView txtShow=(TextView)findViewById(R.id.textView2);
	final RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
	final RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
	final RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
	final RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
  
  int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
   
  String radioButtonSelected = " ";
 	
  switch (checkedRadioButton) {
    case R.id.Answer1 : radioButtonSelected ="Answer1";
    if(position<=Status.size())
	//	txtShow.setText("1");  
    {
    	Status.set(position-1, "1");
    //txtShow.setText(""+Status.elementAt(position-1));
    }
    else{
    	Status.add("1");
	}	
    
             break;
    case R.id.Answer2 : radioButtonSelected = "Answer2";
	
    if(position<=Status.size())
    {
    	Status.set(position-1, "2");
   // txtShow.setText(""+Status.elementAt(position-1));
    }
    else{
	Status.add("2");
    }

         break;

    
    
    
    case R.id.Answer3 : radioButtonSelected = "Answer3";
	if(position<=Status.size())
	{
    	Status.set(position-1, "3");
 //   txtShow.setText(""+Status.elementAt(position-1));
    }
    else
	{
    Status.add("3");
    }	

         break;
    case R.id.Answer4 : radioButtonSelected = "Answer4";
	if(position<=Status.size())
	{
    	Status.set(position-1, "4");
//    txtShow.setText(""+Status.elementAt(position-1));
    }
    		
else{
	Status.add("4");

}	

         break;

//	txtShow.setText(Status.get(3)+"");
	  

  }

 
 // count=Status.size();
  
  
  }  
  
  
  void checkResult()
  {
	  final TextView txtShow=(TextView)findViewById(R.id.textView2);
		  
for(int i=0;i<Status.size();i++)
{
	String s1=Status.get(i).toString();
//	 txtShow.setText("value:"+s1.toString());
	 
  for(int j=0;j<listCorrectAnswer.length;j++)
	  {     String s=Canswer[m].getPosition();
	 if(s1.equals(s))
		  {   String i1=i+"";
			  checkCorrect.add(pos+" ");
			//  txtShow.setText("s1:"+s1);
			  pos=pos+1;
			  count=count+1;
		      break;
		  }
	 else
	 {   String i1=i+"";
	  checkWrong.add(pos+" ");
	  //  txtShow.setText("s1:"+s1);
	  pos=pos+1;
	  countWrong=countWrong+1;
		
	  break;
 }

	 					  
  }
  m=m+1;
}
//	  txtShow.setText(""+s1);

		  
	  //}
 	      
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   CorrectAnswer[] loadPosition()
  {   
	  
	  Canswer  = new CorrectAnswer[100];
		
		for(int i=0;i<listCorrectAnswer.length;i++)
        {
			parts=listCorrectAnswer[i].split(" ");
			question=parts[0];
			answer=parts[1];
			position=parts[2];
			Canswer[i]=new CorrectAnswer();
        	Canswer[i].setQuestion(question);
        	Canswer[i].setAnswerCorrect(answer);
        	Canswer[i].setPosition(position);
        	
		
	
   
    		
        }
	
	  
	  
	  return Canswer;	

  }

  
void loadQuestion(int i)
{   TextView content=(TextView)findViewById(R.id.txtQuestion);
    
    

	content.setText(listQuestion[i]);	

}

	

	
	 
	 



void loadAnswer(int j)
{   

	
final RadioButton Answer1=(RadioButton)findViewById(R.id.Answer1);
final RadioButton Answer2=(RadioButton)findViewById(R.id.Answer2);
final RadioButton Answer3=(RadioButton)findViewById(R.id.Answer3);
final RadioButton Answer4=(RadioButton)findViewById(R.id.Answer4);
RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
//		rb.clearCheck();
//	check();
//if(j>=0 && j<listAnswer.length)
{
	
Answer1.setText(listAnswer[j]);	
Answer2.setText(listAnswer[j+1]);
Answer3.setText(listAnswer[j+2]);
Answer4.setText(listAnswer[j+3]);	
}
					

}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	
 class TimerCounting extends CountDownTimer 
{
			
	public TimerCounting(long millisInFuture, long countDownInterval) {
		super(millisInFuture, countDownInterval);
		// TODO Auto-generated constructor stub

	}

	@Override
	public void onFinish() {
		// TODO Auto-generated method stub
	
	}

	@Override
	public void onTick(long millisUntilFinished) {
		  
		txtTimer.setText("Time :"+  String.valueOf(millisUntilFinished/100));
   		 RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
		 RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
		 RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
		 RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
		 final Button btnNext=(Button)findViewById(R.id.Next);
if (String.valueOf(millisUntilFinished/100).equals("0"))
{	TextView txtShow=(TextView)findViewById(R.id.textView2);
 //   txtShow.setText("Result :"+result+"/"+toTalQuestion);
		rd1.setEnabled(false);
		rd2.setEnabled(false);
		rd3.setEnabled(false);
		rd4.setEnabled(false);
		btnNext.setEnabled(false); 	
		
	}
	}
}
}